﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Oracle.DataAccess.Client;
using log4net;
using System.Reflection;

namespace Cn.Edwon.Data {
    public class ADO4Oracle : AccessSupport, ICommonProcess, ICommonProcedure {
        public static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public ADO4Oracle() { 
        
        }
        public ADO4Oracle(String connString) {
            base.ConnectString = connString;
        }
        public ADO4Oracle(String hostAddress,Int32 port,String sourceName,String user,String password) {
            base.ConnectString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=" + hostAddress + ")(PORT=" + port + ")))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=" + sourceName + ")));User Id=" + user + ";Password=" + password;
        }
        public Int32 CommonUpdateAction(string tsql) {
            this.Sql = tsql;
            log.Debug("tsql:[" + tsql + "]");
            using (OracleConnection conn = new OracleConnection(ConnectString)) {
                log.Debug("Database connect succeed");
                OracleCommand odc = new OracleCommand(tsql, conn);
                conn.Open();
                log.Debug("Connection opened");
                return odc.ExecuteNonQuery();
            }
        }
        public DataSet CommonSelectAction(string tsql, string dataname) {
            this.Sql = tsql;
            log.Debug("tsql:[" + tsql + "]");
            using (OracleConnection conn = new OracleConnection(ConnectString)) {
                log.Debug("Database connect succeed");
                using (OracleDataAdapter da = new OracleDataAdapter(tsql, conn)) {
                    if (da != null) {
                        log.Debug("DataAdapter allready");
                    } else {
                        log.Error("DataAdapter error");
                        return null;
                    }
                    DataSet ds = new DataSet();
                    da.Fill(ds, dataname);
                    log.Debug("Fill data to dataset succeed");
                    return ds;
                }
            }
        }


        #region ICommonProcedure 成员


        public void ExecuteProc(string rProcName, ref Object rOutObj, ref Object rReturnObj, params object[] rParams) {
            log.Debug("Proc:[" + rProcName + "]");
            using (OracleConnection conn = new OracleConnection(ConnectString)) {
                log.Debug("Database connect succeed");
                OracleCommand command = new OracleCommand(rProcName, conn);
                command.CommandType = CommandType.StoredProcedure;
                OracleParameter rep = new OracleParameter("ReturnValue", DbType.Object);
                rep.Direction = ParameterDirection.ReturnValue;
                command.Parameters.Add(rep);
                OracleParameter oup = new OracleParameter("OutValue", DbType.Object);
                oup.Direction = ParameterDirection.Output;
                command.Parameters.Add(oup);
                //
                if (rParams != null) {
                    if ((command.Parameters.Count - 1) != rParams.Length) {
                        throw new ArgumentNullException("The number of parameters does not match number of values for stored procedure.");
                    }
                    for (int i = 0; i < rParams.Length; i++) {
                        command.Parameters[i + 1].Value = (rParams[i] == null) ? DBNull.Value : rParams[i];
                    }
                }

                command.ExecuteNonQuery();
                rOutObj = command.Parameters["OutValue"];
                rReturnObj = command.Parameters["ReturnValue"];
            }
        }

        public void ExecuteProc(string rProcName, ref Object rOutObj, ref Object rReturnObj, IList<object> rParams) {
            log.Debug("Proc:[" + rProcName + "]");
            using (OracleConnection conn = new OracleConnection(ConnectString)) {
                log.Debug("Database connect succeed");
                OracleCommand command = new OracleCommand(rProcName, conn);
                command.CommandType = CommandType.StoredProcedure;
                OracleParameter rep = new OracleParameter("ReturnValue", DbType.Object);
                rep.Direction = ParameterDirection.ReturnValue;
                command.Parameters.Add(rep);
                OracleParameter oup = new OracleParameter("OutValue", DbType.Object);
                oup.Direction = ParameterDirection.Output;
                command.Parameters.Add(oup);
                //
                if (rParams != null) {
                    if ((command.Parameters.Count - 1) != rParams.Count) {
                        throw new ArgumentNullException("The number of parameters does not match number of values for stored procedure.");
                    }
                    for (int i = 0; i < rParams.Count; i++) {
                        command.Parameters[i + 1].Value = (rParams[i] == null) ? DBNull.Value : rParams[i];
                    }
                }

                command.ExecuteNonQuery();
                rOutObj = command.Parameters["OutValue"];
                rReturnObj = command.Parameters["ReturnValue"];
            }
        }

        #endregion
    }
}
